package com.zhiguang.cms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhiguang.cms.entity.Article;
import com.zhiguang.cms.entity.Link;
import com.zhiguang.cms.entity.User;
import com.zhiguang.cms.service.ArticleService;
import com.zhiguang.cms.service.LinkService;
import com.zhiguang.cms.service.UserService;

@RequestMapping("admin")
@Controller
public class AdminController {
	
	@Autowired
	UserService userService;
	
	@Autowired
	ArticleService articleService;
	
	/**
	 * 友情链接
	 */
	@Autowired
	LinkService linkService;
	
	@RequestMapping("index")
	public String index() {
		return "admin/index";
	}
	
	/**
	 * 
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping("userlist")
	public String userlist(HttpServletRequest request,
			@RequestParam(defaultValue="1") int page) {
		
		PageInfo<User>  userPage = userService.list(page);
		request.setAttribute("userPage", userPage);
		
		return "admin/userlist";
	}
	
	/**
	 * 
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping("articlelist")
	public String articlelist(HttpServletRequest request,
			@RequestParam(defaultValue="1") int page) {
		
		PageInfo articlePage = articleService.list(page);
		
		List list = articlePage.getList();
		list.forEach(ar->{
			
			System.out.println("article is " + ar);
		});
		
		request.setAttribute("articlePage", articlePage);
		
		return "admin/articlelist";
	}
	
	/**
	 * 
	 * @param userid
	 * @param status
	 * @return
	 */
	@RequestMapping("userlock")
	@ResponseBody
	public String userlock(int userid,int status) {
		System.out.println("处理成功");
		int re = userService.lock(userid,status);
		return re>0?"success":"failed";
	}
	
	@RequestMapping("article")
	public String article(HttpServletRequest request,int id) {
		
		Article article = articleService.getById(id);
		request.setAttribute("article", article);
		return "admin/articleDetail";
		
		
	}
	
	/**
	 * 
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping("checkArticle")
	@ResponseBody
	public String checkArticle(int id,int status) {
		//审核文章 >0  操作成功
		int result = articleService.check(id,status);
		return result>0?"success":"failed";
		
	}
	

	/**
	 * 
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping("hotArticle")
	@ResponseBody
	public String hotArticle(int id,int status) {
		//审核文章 >0  操作成功
		int result = articleService.hot(id,status);
		return result>0?"success":"failed";
		
	}
	
	/////////////////////  友情链接管理   ///////////////////////////////////
	@RequestMapping("linklist")
	public String linklist(HttpServletRequest request,
			@RequestParam(defaultValue="1") int page ) {
		
		PageInfo<Link> pageLink=  linkService.list(page);
		request.setAttribute("pageLink", pageLink);
		return "admin/linklist";
		
	}
	
	@RequestMapping(value="addlink")
	public String addlink(HttpServletRequest request) {
		return "admin/link/add";
	}
	
	@RequestMapping("addLinkData")
	@ResponseBody
	public String addLinkData(HttpServletRequest request,Link link) {
		int result = linkService.add(link);
		if(result<1)
			return "failed";
		
		return "success";
	}
	
	@RequestMapping("delLink")
	@ResponseBody
	public String delLink(HttpServletRequest request,int id) {
		int result = linkService.delLink(id);
		if(result<1)
			return "failed";
		return "success";
	}
	
	/**
	 * 显示修改的页面
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("toUodatelink")
	public String toUodatelink(HttpServletRequest request,int id) {
		Link link = linkService.get(id);
		request.setAttribute("link", link);
		return "admin/link/update";
	}
	
	@RequestMapping("updateLinkData")
	@ResponseBody
	public String updateLinkData(HttpServletRequest request,Link link) {
		int result = linkService.update(link);
		return result>0?"success":"failed";
		
	}
	
	
		
	

}
